<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\we\model;

use think\Model;

class WeFansBuy extends Model
{
    protected $insert = ['status'=>1]; 
    
    /**
     * 获取微信配置信息，需要缓存
     */
    public function info($openid,$field=true){
      
      if (is_numeric($openid)){
        $map['id']=$openid;
      }else{
        $map['openid']=$openid;
      }
      $fans = $this->field($field)->where($map)->find();
     
      return  $fans;
    }

     public function infoForFans($openid,$field=true){
      $fans=$this->info($openid,$field=true);
      if (!$fans) return false;
      $printer=model('printer/PrinterFans')->info($fans['openid'],'balance');

      $message="粉丝信息";
      $message.="\nID：".$fans['id'];
      $message.="\n昵称：".$fans['nickname'];
      if($printer) $message.="\n打印余额：".$printer['balance'];
      return  ['fans'=>$fans,'message'=>$message];
    }

    public function infoForFansByOrder($data){
     $fans = $this->infoForFans($data['before']);
     if (!$fans) return ['message'=>'粉丝不存在，请核对粉丝ID'];
     return ['message'=>$fans['message']]; //空尾巴查询
    }

     public function getAdmins($appid){
      $map['appid']=$appid;
      $map['userid']=['NEQ',''];
     
      $admins=$this->where($map)->select();

      
      return  $admins;
    }




    public function editData($data){

      
      if (isset($data['id'])){
         $res = $this->allowField(true)->where('id',$data['id'])->update($data);
      }else{
         $res = $this->allowField(true)->data($data)->save();
      }

      return $res;
    }

    public function buy($data){
  
        $fans=model('we/WeFans')->info($data['before']);
       
        if (!$fans)return ['message'=>$data['before'].'号粉丝不存在'];
        if(strstr($data['back'],'++')){
            $desk = substr($data['back'],0,strrpos($data['back'],'++'));
            $price=str_replace($desk.'++','',$data['back']);
          }else{
            $desk = substr($data['back'],0,strrpos($data['back'],'+'));
            $price=str_replace($desk.'+','',$data['back']);
        }

        //数据有效性验证
        if (!$desk ) return ['message'=>'桌号没有输入'];
        if (!$price) return ['message'=>'消费金额没有输入'];
          
        $buy=[];

        //积分
        if(strstr($data['back'],'++')){
          $score_message=model('we/WeFansCfo')->cfoScore($fans,1,$data);
          if ($score_message)$buy['score']=1;
        }

        //分销提成
        if($fans['subscribe_openid']){
          $father_fans=model('we/WeFans')->info($fans['subscribe_openid']);
          $amount_message=model('we/WeFansCfo')->cfoAmount($father_fans,0.1,$data);
          if ($amount_message){
            $buy['reward']=0.1;
            $buy['reward_openid']=$fans['subscribe_openid'];
          }
        }
        
         $buy['aid']=$data['aid'];
         $buy['appid']=$data['appid'];
         $buy['shopid']=$data['fans']['admin_info']['shopid'];
         $buy['fans']=$fans['id'];
         $buy['openid']=$fans['openid'];
         $buy['nickname']=$fans['nickname'];
         $buy['admin']=$data['admin'];
         $buy['name']=$data['fans']['admin_info']['name'];
         $buy['desk']=$desk;
         $buy['price']=$price;

         
         //todo 防止重复录入
         $res=$this->editData($buy);
         if ($res) $message=$fans['id'].'号顾客'.$fans['nickname'].'在'.$desk.'号桌消费'.$price.'元，录入成功'.(isset($buy['score'])?'，已为顾客积分！':'');
         return $message;
    }

    
  
    

    
}